
import re
import string

# czech woman to man surname conversion routines

"""
-- DROP FUNCTION surname_cz(text, text);

CREATE OR REPLACE FUNCTION surname_cz(surname text, operation text)
  RETURNS text AS
$BODY$
  s = args[0]; o = args[1]

  import pepe.p_dm.dm_surname_cz as sn

  if o in ('m2w'):
    r = sn.m2w_main(s, methods=('generic'))
  elif o in ('w2m'):
    r = sn.w2m_main(s, methods=('generic'))
  else:
    r = ''

  return r
$BODY$
  LANGUAGE plpythonu VOLATILE;
ALTER FUNCTION surname_cz(text, text) OWNER TO dm;
"""

def w2m_file():
    r = {}
    o = open('cz_surnames_manual_110531vp.lkp', 'r')
    pairs = [re.sub('\n', '', x) for x in o.readlines()]
    o.close()
    for p in pairs:
        m,w = string.split(p, '\t')
        r[w] = m
    return r
'''
if 0:
    # buffered lookup 
    # to be loaded once only at module import for performance
    w2m_file_lookup = w2m_file() 
    if 0: print w2m_file_lookup['BERKOVA'] # testing
'''
"""
def w2m_fixed_pairs(surname):
    ''' woman to man surname (fixed) mapping
    '''
    sm = surname[:]
    # full names, special common cases
    if   sm == 'BARTOVA': r = 'BARTA'
    elif sm == 'BILKOVA': r = 'BILEK'  # alt.BILKA
    elif sm == 'BOHACOVA': r = 'BOHAC'
    elif sm == 'BUCHTOVA': r = 'BUCHTA'
    elif sm == 'CHLADKOVA': r = 'CHLADEK'
    elif sm == 'CIZEK': r = 'CIZKOVA'
    elif sm == 'DUSKOVA': r = 'DUSEK'
    elif sm == 'FIALOVA': r = 'FIALA'
    elif sm == 'HAJKOVA': r = 'HAJEK'
    elif sm == 'HAVLICKOVA': r = 'HAVLICEK'
    elif sm == 'HOSKOVA': r = 'HOSEK'
    elif sm == 'HLAVACKOVA': r = 'HLAVACEK' # alt.HLAVACKA
    elif sm == 'JEZKOVA': r = 'JEZEK'
    elif sm == 'KANOVA': r = 'KANA'
    elif sm == 'KLIMOVA': r = 'KLIMA'
    elif sm == 'KOZLOVA': r = 'KOZEL'
    elif sm == 'KREJZCOVA': r = 'KREJCA' # alt. KREJCI
    elif sm == 'KREJCOVA': r = 'KREJCA' # alt. KREJCI
    elif sm == 'KROUPOVA': r = 'KROUPA'
    elif sm == 'KUBICKOVA': r = 'KUBICEK' # alt. KUBICKA
    elif sm == 'KUCEROVA': r = 'KUCERA'
    elif sm == 'LUKASKOVA': r = 'LUKASEK'
    elif sm == 'MARKOVA': r = 'MAREK'
    elif sm == 'MASKOVA': r = 'MASEK' # alt. MASKA
    elif sm == 'PAGACOVA': r = 'PAGAC'
    elif sm == 'PLESKACOVA': r = 'PLESKAC'
    elif sm == 'SALKOVA': r = 'SALKA' # alt. SALEK
    elif sm == 'SIKOROVA': r = 'SIKORA'
    elif sm == 'SIMOVA': r = 'SIMA'
    elif sm == 'SLADKOVA': r = 'SLADEK'
    elif sm == 'SLOVACKOVA': r = 'SLOVACEK'
    elif sm == 'SMIDOVA': r = 'SMID' # alt. SMIDA
    elif sm == 'STRAKOVA': r = 'STRAKA'
    elif sm == 'SVARCOVA': r = 'SVARC'
    elif sm == 'TAKACOVA': r = 'TAKAC'
    elif sm == 'TKACOVA': r = 'TKAC'
    elif sm == 'TOMASKOVA': r = 'TOMASEK' # alt.TOMASKA
    elif sm == 'TUMOVA': r = 'TUMA'
    elif sm == 'VALENTOVA': r = 'VALENTA'
    elif sm == 'VACKOVA': r = 'VACEK'
    elif sm == 'VRANOVA': r = 'VRANA'
    elif sm == 'VACOVA': r = 'VACA'
    elif sm == 'VICHORCOVA': r = 'VICHOREC'

    return sm
"""

def m2w_generic(surname):
    ''' '''
    sm = surname[:].upper()
    r = ''

    if   sm in ('JANU'): # JANU -> JANU
        r = sm[:]
    elif sm[-4:] == 'ABEC': # VRABEC -> VRABCOVA
        r = sm[:-4] + 'ABCOVA'
    elif sm[-4:] == 'AZKA': # PROCHAZKA -> PROCHAZKOVA
        r = sm[:-2] + 'KOVA'
    elif sm[-4:] == 'BSKY': # DUBSKY -> DUBSKA
        r = sm[:-4] + 'BSKA'
    elif sm[-4:] == 'CERA': # KUCERA -> KUCEROVA
        r = sm[:-4] + 'CEROVA'
    elif sm[-4:] == 'ECKY': # KOPECKY -> KOPECKA
        r = sm[:-4] + 'ECKA'
    elif sm[-4:] == 'EMEC': # NEMEC -> NEMCOVA
        r = sm[:-4] + 'EMCOVA'
    elif sm[-4:] == 'ICHY': # TICHY -> TICHA
        r = sm[:-4] + 'ICHA'
    elif sm[-4:] == 'NDRA': # JINDRA -> JINDROVA
        r = sm[:-4] + 'NDROVA'
    elif sm[-4:] == 'OHAC': # BOHAC -> BOHACOVA
        r = sm[:-4] + 'OHACOVA'
    elif sm[-4:] == 'ORNY': # POKORNY -> POKORNA
        r = sm[:-4] + 'ORNA'
    elif sm[-4:] == 'OUHY': # DLOUHY -> DLOUHA
        r = sm[:-4] + 'OUHA'
    #-
    elif sm[-3:] == 'ANA': # KANA -> KANOVA
        r = sm[:-3] + 'ANOVA'
    elif sm[-3:] == 'ALA': # FIALA -> FIALOVA
        r = sm[:-3] + 'ALOVA' # SKALA -> SKALOVA
    elif sm[-3:] == 'CHA': # PECHA -> PECHOVA
        r = sm[:-3] + 'CHOVA' # VACHA -> VACHOVA
    elif sm[-3:] == 'CKA': # RUZICKA -> RUZICKOVA
        r = sm[:-3] + 'CKOVA'   
    elif sm[-3:] == 'IHA': # RIHA -> RIHOVA
        r = sm[:-3] + 'IHOVA'   
    elif sm[-3:] == 'IMA': # SIMA -> SIMOVA
        r = sm[:-3] + 'IMOVA'   
    elif sm[-3:] == 'IKA': # ZIKA -> ZIKOVA
        r = sm[:-3] + 'IKOVA'   
    elif sm[-3:] == 'JCI': # KREJCI -> KREJCOVA
        r = sm[:-3] + 'JCOVA'   
    elif sm[-3:] == 'RKY': # HORKY -> HORKA
        r = sm[:-3] + 'RKA'   
    #-
    elif sm[-2:] == 'BA': # KOUBA -> KOUBOVA
        r = sm[:-2] + 'BOVA'   
    elif sm[-2:] == 'DA': # SVOBODA -> SVOBODOVA
        r = sm[:-2] + 'DOVA'   
    elif sm[-2:] == 'EK': # MAREK -> MARKOVA
        r = sm[:-2] + 'KOVA'
    elif sm[-2:] == 'LY': # VESELY -> VESELA
        r = sm[:-2] + 'LA'   
    elif sm[-2:] == 'NY': # STASTNY -> STASTNA
        r = sm[:-2] + 'NA'
    #-
    elif sm[:] == 'NOVY': # NOVY -> NOVA
        r = 'NOVA'
    #-
    else:
        r = sm + 'OVA'

    return r

if 0:
    #print m2w_generic('SVOBODA')
    print m2w_generic('NOVOTNY')

def w2m_generic(surname):
    ''' '''
    sm = surname[:]

    # generalized approach based on appendix

    if sm == 'NOVA': r = 'NOVY' # NOVA -> NOVY

    elif sm[-6:] == 'ACKOVA': # SEDLACKOVA -> SEDLACEK
        r = sm[:-6] + 'ACEK'  
    elif sm[-6:] == 'ARKOVA': # MARKOVA -> M MAREK
        r = sm[:-6] + 'AREK'
    elif sm[-6:] == 'CEROVA': # KUCEROVA -> KUCERA
        r = sm[:-6] + 'CERA'  
    elif sm[-6:] == 'EJCOVA': # KREJCOVA -> KREJCI
        r = sm[:-6] + 'EJCI'
    elif sm[-6:] == 'IALOVA': # FIALOVA -> FIALA
        r = sm[:-6] + 'IALA'
    elif sm[-6:] == 'ICKOVA': # VODICKOVA -> VODICKA
        r = sm[:-6] + 'ICKA'
    elif sm[-6:] == 'LIKOVA': # STUCHLIKOVA -> STUCHLIK
        r = sm[:-6] + 'LIK'
    elif sm[-6:] == 'MANOVA': # HARTMANOVA -> HARTMAN
        r = sm[:-6] + 'MAN'  
    #-
    elif sm == 'HALOVA': r = 'HALA' # HALOVA -> HALA exception from below
    elif sm == 'KALOVA': r = 'KALA' # KALOVA -> KALA exception from below
    elif sm[-5:] == 'ALOVA': # KRALOVA -> KRAL
        r = sm[:-5] + 'AL'
    elif sm[-5:] == 'ANOVA': # KANOVA -> KANA
        r = sm[:-5] + 'ANA'
    elif sm[-5:] == 'AVOVA': # AVOVA : w DOUBRAVOVA == m DOUBRAVA
        r = sm[:-5] + 'AVA'
    elif sm[-5:] == 'AROVA': # KOLAROVA -> KOLAR
        r = sm[:-5] + 'AR'
    elif sm[-5:] == 'BKOVA': # BKOVA : w HRBKOVA == m HRBEK
        r = sm[:-5] + 'BEK'
    elif sm[-5:] == 'CHOVA': # CECHOVA -> CECH
        r = sm[:-5] + 'CH'
    elif sm[-5:] == 'CKOVA': # JURECKOVA -> JURECKA
        r = sm[:-5] + 'CKA'
    elif sm[-5:] == 'DROVA': # SNAJDROVA -> SNAJDR
        r = sm[:-5] + 'DR'
    elif sm[-5:] == 'ECOVA': # ECOVA : w KADLECOVA == m KADLEC
        r = sm[:-5] + 'EC'   #         w SVECOVA == m SVEC
    elif sm[-5:] == 'ELOVA': # KYSELOVA -> KYSELA
        r = sm[:-5] + 'ELA'   
    elif sm[-5:] == 'EROVA': # NEUBAUEROVA -> NEUBAUER
        r = sm[:-5] + 'ER'
    elif sm[-5:] == 'FKOVA': # KAFKOVA -> KAFKA
        r = sm[:-5] + 'FKA'   
    elif sm[-5:] == 'IHOVA': # RIHOVA -> RIHA
        r = sm[:-5] + 'IHA'
    elif sm[-5:] == 'IKOVA': # MIKOVA -> MIKA
        r = sm[:-5] + 'IKA'
    elif sm[-5:] == 'ILOVA': # POSPISILOVA -> POSPISIL
        r = sm[:-5] + 'IL'
    elif sm[-5:] == 'INOVA': # SLANINOVA -> SLANINA
        r = sm[:-5] + 'INA'
    elif sm[-5:] == 'JKOVA': # HAJKOVA -> HAJEK
        r = sm[:-5] + 'JEK'
    elif sm[-5:] == 'LKOVA': # LKOVA : w MYLKOVA == m MYLKA
        r = sm[:-5] + 'LKA'
    elif sm[-5:] == 'MCOVA': # MCOVA : w SAMCOVA == m SAMEC
        r = sm[:-5] + 'MEC'  # NEMCOVA -> NEMEC
    elif sm[-5:] == 'MKOVA': # MKOVA : w SRAMKOVA == m SRAMEK
        r = sm[:-5] + 'MEK'
    elif sm[-5:] == 'NKOVA': # NKOVA : w PANKOVA == m PANEK
        r = sm[:-5] + 'NEK'
    elif sm[-5:] == 'VCOVA': # VCOVA : w BUDOVCOVA == m BUDOVEC
        r = sm[:-5] + 'VEC'
    elif sm[-5:] == 'USOVA': # PRUSOVA -> PRUSA
        r = sm[:-5] + 'USA'
    elif sm[-5:] == 'OTOVA': # OTOVA : w DRAHOTOVA == m DRAHOTA
        r = sm[:-5] + 'OTA'
    elif sm[-5:] == 'PKOVA': # PKOVA : w LIPKOVA == m LIPKA
        r = sm[:-5] + 'PKA'
    elif sm[-5:] == 'RBOVA': # STERBOVA -> STERBA
        r = sm[:-5] + 'RBA'
    elif sm[-5:] == 'RKOVA': # RKOVA : w UHYRKOVA == m UHYORKA
        r = sm[:-5] + 'RKA'  #         w BERKOVA == m BERKA
    elif sm[-5:] == 'RLOVA': # RLOVA : w VORLOVA == m VOREL
        r = sm[:-5] + 'REL'
    elif sm[-5:] == 'SKOVA': # HANOUSKOVA -> HANOUSEK
        r = sm[:-5] + 'SEK'
    elif sm[-5:] == 'TKOVA': # TKOVA : w VOJTKOVA == m VOJTEK
        r = sm[:-5] + 'TEK'
    elif sm[-5:] == 'THOVA': # TOTHOVA -> TOTH
        r = sm[:-5] + 'TH'
    elif sm[-5:] == 'UBOVA': # KOUBOVA -> KOUBA
        r = sm[:-5] + 'UBA'
    elif sm[-5:] == 'ULOVA': # MATULOVA -> MATULA
        r = sm[:-5] + 'ULA'
    elif sm[-5:] == 'UPOVA': # CHALUPOVA -> CHALUPA
        r = sm[:-5] + 'UPA'
    elif sm[-5:] == 'VKOVA': # VKOVA : w STAVKOVA == m STAVKA
        r = sm[:-5] + 'VKA'
    elif sm[-5:] == 'YLOVA': # PRIBYLOVA -> PRIBYL
        r = sm[:-5] + 'YL'
    elif sm == 'BLAZKOVA':   # BLAZKOVA -> BLAZEK conflict see rule below
        r = 'BLAZEK'
    elif sm[-5:] == 'ZKOVA': # VODRAZKOVA -> m VODRAZKA
        r = sm[:-5] + 'ZKA'
    #-
    elif sm[-4:] == 'COVA': # COVA : w CHOVANCOVA == m CHOVANEC
        r = sm[:-4] + 'EC'
    elif sm[-4:] == 'DOVA': # DOVA : w HOJDOVA == m HOJDA
        r = sm[:-4] + 'DA'
    elif sm[-4:] == 'GOVA': # GOVA : w VARGOVA == m VARGA
        r = sm[:-4] + 'GA'
    elif sm[-4:] == 'HOVA': # HOVA : w PYCHOVA == m PYCHA
        r = sm[:-4] + 'HA'
    elif sm[-4:] == 'ROVA': # KUCEROVA -> KUCERA
        r = sm[:-4] + 'RA'  
    #-
    elif sm[-3:] == 'OVA': # OVA : w NOVAKOVA == m NOVAK
        r = sm[:-3]
    elif sm[-3:] <> 'OVA' \
        and sm[-1:] == 'A': # A : w STARA == m STARY
        r = sm[:-1] + 'Y'
    elif sm[-3:] == 'SKA': # SKA : w ZDARSKA == m ZDARSKY
        r = sm[:-3] + 'SKY'

    else: # w JANKU, PETRU = m JANKU, PETRU
        r = sm

    return r


def w2m_main(surname, methods=('generic')):
    ''' cz woman to man surnames conversion routine
        combining methods (file, fixed pairs, generic) above
    '''
    s = surname[:] # separate original submitted value

    # try to find surname in special file for mapping
    '''
    if 'file' in methods:
        #if w2m_file_lookup.has_key(s):
        if w2m_file().has_key(s)
            s = w2m_file_lookup[s]
    '''
    '''
    # try fixed special pairs
    if s == surname and 'fxpairs' in methods:
        # only if unchanged by previous steps
        # those not handled well in next generic step mapping
        s = w2m_fixed_pairs(s)
    '''
    # generic py mapping
    if s == surname and 'generic' in methods:
        # only if unchanged by previous steps
        # try generic (guessing) woman2man routine
        s = w2m_generic(surname)

    return s


def m2w_main(surname, methods=('generic')):
    ''' cz woman to man surnames conversion routine
        combining methods (file, fixed pairs, generic) above
    '''
    if 'generic' in methods:
        return m2w_generic(surname)
